package com.app.mymusic.ui;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.app.mymusic.R;
import com.app.mymusic.adapter.DownloadingAdapter;
import com.app.mymusic.model.Mp3Info;
import com.app.mymusic.model.Mp3InfoDao;
import com.app.mymusic.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/9.
 */
public class DownLoadingFragment extends Fragment {
    private TextView tv;
    private RecyclerView mRecyclerView;
    private List<Mp3Info> mDatas;
    private DownloadingAdapter mAdapter;
    private Mp3InfoDao mp3Dao;
    private Button b1;

    private Handler mHandler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 110:
                    mRecyclerView.setAdapter(mAdapter);
                    break;
                case 111:
                    mAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    public DownLoadingFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtil.showLog("i--onCreateView");
        View view=inflater.inflate(R.layout.downloadingfragment,container,false);
        tv= (TextView) view.findViewById(R.id.f2);
        mRecyclerView= (RecyclerView) view.findViewById(R.id.ing_recyclerview);
        b1= (Button) view.findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(Mp3Info m:mp3Dao.getAll())
                {
                    LogUtil.showLog(m.getTitle());
                }
            }
        });
        LinearLayoutManager manager=new LinearLayoutManager(getActivity().getApplicationContext());
        mRecyclerView.setLayoutManager(manager);
        return view;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.showLog("i--ondestroy");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtil.showLog("i--onDetach");
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtil.showLog("i--onStart");
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.showLog("i--onCreate");
        EventBus.getDefault().register(this);
        mp3Dao=new Mp3InfoDao(getActivity());
        mDatas=new ArrayList<Mp3Info>();
    }

    @Subscribe
    public void onEventMainThread(Mp3Info mp3Info)
    {
        Message msg= mHandler.obtainMessage();
        if (mDatas.size()==0)
        {
            msg.what=110;
            mHandler.sendMessage(msg);
            mDatas.add(mp3Info);
            mAdapter=new DownloadingAdapter(R.layout.item_downloadingfragment,mDatas);
        }
        else{
            msg.what=111;
            mHandler.sendMessage(msg);
        }
        if(mp3Info.isDone())
        {
            mp3Dao.addMp3Info(mp3Info);
        }
    }
}
